<?php
/**
 * 用户上传文件
 * 文件名生成规则：$uid_$fid
 */
class Data_File
{
    /**
     * $force   1 满足宽度即可
     *          2 满足高度即可
     *          3 同时满足高度和宽度
     */
    private static $picConf = array(
        '' => array('width' => 1024, 'height' => 768, 'force' => 0),
        '120x90' => array('width' => 120, 'height' => 90, 'force' => 0),
        '170x170' => array('width' => 170, 'height' => 170, 'force' => 0),	//专辑列表,照片列表
        '280x280' => array('width' => 280, 'height' => 280, 'force' => 0),
        '580x387' => array('width' => 580, 'height' => 387, 'force' => 1),
        '650x480' => array('width' => 650, 'height' => 480, 'force' => 1),
    );

    const SUB_PATH = 'ufile';

    /**
     * @param file 本地文件
     */
    public function savePic($file, $uid, $pid, $info=array())
    {
        if (empty($info))
        {
            $info = Tool_Image::getImageInfo($file);
        }

        $path = self::getPathById($uid);
        $filename = $uid .'_'. $pid .'.'. $info['type'];
        $desPath = STORAGE_PATH . self::SUB_PATH . DS . $path;
        $desPath = str_replace('/', DS, $desPath);
        if (!file_exists($desPath))
        {
            mkdir($desPath, 0777, true);
        }

        $desFile = $desPath . $filename;
        $ret = Tool_Image::convertImage($file, $info, $desFile,
                        self::$picConf['']['width'],
                        self::$picConf['']['height'],
                        self::$picConf['']['force'], false, true, 0, true);

        return $ret;
    }

    /**
     * 获取文件内容
     * @param file 本地文件
     */
    public function getPic($url, &$pid)
    {
        // 从url中提取参数
        $filename = basename($url);
        list($token, $type) = explode('.', $filename);
        list($uid, $pid, $size) = explode('_', $token);

        // 从本地文件中读数据
        $path = self::getPathById($uid);
        $localFile = STORAGE_PATH . self::SUB_PATH . $path . $filename;
        $localFile = str_replace('/', DS, $localFile);

        if (file_exists($localFile))
        {
            $content = file_get_contents($localFile);
            if (false != $content)
            {
                return $content;
            }
        }

        // 用源文件缩略
        $filename = $uid .'_'. $pid .'.'. $type;
        $srcFile = STORAGE_PATH . self::SUB_PATH . $path . $filename;
        $srcFile = str_replace('/', DS, $srcFile);
        $info = Tool_Image::getImageInfo($srcFile);
        if (!empty($info))
        {
            $ret = Tool_Image::convertImage($srcFile, $info, $localFile,
                            self::$picConf[$size]['width'],
                            self::$picConf[$size]['height'],
                            self::$picConf[$size]['force'], false, true, 0, true);
            if ($ret)
            {
                $content = file_get_contents($localFile);
                return $content;
            }
        }

        return false;
    }


    /**
     * 输入: 图片组id, 图片序号id, 图片规格('20x20')
     * 输出: 图片的url
     */
    public static function getPicUrl($uid, $pid, $size='')
    {
        $fakeType = '.jpg';
        $userInfo = new User_File();
        $pic = $userInfo->getPicInfo($uid, $pid);
        if(!empty($pic))
            $fakeType = '.'.$pic['type'];

        $path = self::getPathById($uid);
        if(empty($size))
        {
            return 'http://' . PIC_HOST . '/pic/' . $uid . '_' . $pid . $fakeType;
        }
        else
        {
            return 'http://' . PIC_HOST . '/pic/' . $uid . '_' . $pid . '_' . $size . $fakeType;
        }
    }

    public static function getLocalFilename($uid, $pid, $type)
    {
        $path = STORAGE_PATH . self::SUB_PATH . DS . self::getPathById($uid);
        $filename = $uid .'_'. $pid .'.'. $type;
        return $path . $filename;
    }

    private static function getPathById($uid)
    {
        $path = DS;
        $path .= (floor($uid / 10000) % 100) . DS;
        $path .= (floor($uid / 100) % 100) . DS;
        return $path;
    }
}
?>